package com.group.controller;

import com.group.service.ClientService;
import com.group.user.ChaVo;
import com.group.user.ClientVo;
import com.group.user.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/exam")
public class ClientController {

    @Autowired
    private ClientService clientService;

    @RequestMapping("login.do")
    public String tologin(){
        return "login";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public int login(UserVo userVo){
        //clientService.login(userVo);
        if (userVo!=null){
            return 0;
        }
        return 1;
    }

    @RequestMapping("/registered")
    public String registered(){
        return "index";
    }

    //会员列表查询
    @RequestMapping("/memberlist")
    public String memberlist(ChaVo chaVo,Model model){
        List list = clientService.memberlist(chaVo);
        model.addAttribute("li",list);
        model.addAttribute("nickname",chaVo.getNickname());
        model.addAttribute("start",chaVo.getStart());
        model.addAttribute("end",chaVo.getEnd());
        return "member-list";
    }

    //会员修改密码和编辑回显
    @RequestMapping("/memberto")
    public String memberto(int id,Model model){
        ClientVo clientVo = clientService.memberto(id);
        model.addAttribute("to",clientVo);
        return "member-edit";
    }

    //会员修改密码和编辑回显
    @RequestMapping("/memberpass/{id}")
    public String memberpass(@PathVariable("id") int id,Model model){
        ClientVo clientVo = clientService.memberpass(id);
        model.addAttribute("pass",clientVo);
        return "member-password";
    }

    @RequestMapping("/memberpassxiu")
    public String memberpassxiu(ClientVo clientVo){
        //clientService.memberpassxiu(clientVo);
        return "member-password";
    }

    //会员添加
    @RequestMapping("/memberadd")
    @ResponseBody
    public int memberadd(ClientVo clientVo){
        try {
            clientService.memberadd(clientVo);
            return 0;
        }catch (Exception e){
            return 1;
        }
    }

    //会员删除
    @RequestMapping("/memberdel")
    @ResponseBody
    public Boolean memberdel(int id){
        try {
            clientService.memberdel(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    //会员批量删除
    @RequestMapping("/delAll")
    @ResponseBody
    public int delAll(String ids){
        String s = ids.substring(0, ids.length() - 1);
        try {
            clientService.delAll(s);
            return 0;
        }catch (Exception e){
            return 1;
        }
    }

    //会员列表查询
    @RequestMapping("/memberstatus")
    @ResponseBody
    public int memberstatus(int id,int status,Model model){
        try {
            clientService.memberstatus(id,status);
            return 0;
        }catch (Exception e){
            return 1;
        }
    }

    //测试
    @RequestMapping("/ss")
    public void ss(){
        List list = clientService.list();
        System.out.println(list);
    }
}
